home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / Pascal OS8 / Temperature / MainMenu.p < prev    next >
Encoding:
Text File  |  1998-10-30  |  4.9 KB  |  254 lines  |  [TEXT/CWIE]

  1. { MainMenu.p }
  2. { Created 10/30/98 12:58 PM by AppMaker }
  3.  
  4. Unit MainMenu;
  5. Interface
  6.  
  7. Uses
  8.     Types,
  9.     Quickdraw,
  10.     Controls,
  11.     Dialogs,
  12.     Events,
  13.     Lists,
  14.     LowMem,
  15.     Menus,
  16.     TextEdit;
  17.  
  18. {----------}
  19. Procedure InitTitles;
  20. Procedure LoadMenus;
  21. Procedure DoMenu        (menuChoice:    longint);
  22. Procedure UpdateMenus;
  23.  
  24. {----------}
  25. Implementation
  26.  
  27. Uses
  28.     Devices,
  29.     Globals,
  30.     ResourceDefs,
  31.     Miscellany,
  32.     AMApp,
  33.     AMDoc,
  34.     AMEngine,
  35.     AMWindow;
  36.  
  37. {----------}
  38. Function  GetCommandFromMenu (
  39.     menuChoice:    longint): longint; Forward;
  40. Procedure DoApple (
  41.     itemNr:        integer); Forward;
  42.  
  43. {----------}
  44. Procedure InitTitles;
  45. Begin
  46. end; {InitTitles}
  47.  
  48. {----------}
  49. Procedure LoadMenus;
  50. Begin
  51.     AppleMenu    := GetMenu (MENU_Apple);
  52.     FailNilResource (Handle (AppleMenu));
  53.     AppendResMenu (AppleMenu, 'DRVR');
  54.     FileMenu    := GetMenu (MENU_File);
  55.     EditMenu    := GetMenu (MENU_Edit);
  56.  
  57.     InsertMenu (AppleMenu, 0);
  58.     InsertMenu (FileMenu, 0);
  59.     InsertMenu (EditMenu, 0);
  60.  
  61.     DrawMenuBar;
  62. end; {LoadMenus}
  63.  
  64. {----------}
  65. Procedure DoMenu (
  66.     menuChoice:        longint);
  67. var
  68.     commandID:        longint;
  69.     curDoc:            AMDoc;
  70.     menuID:            integer;
  71.     itemNr:            integer;
  72. begin
  73.     commandID := GetCommandFromMenu (menuChoice);
  74.     curDoc := cur.mDoc;
  75.     if cur.DoCommand (commandID) then begin
  76.         { cur window handled it }
  77.     end else if (curDoc <> nil)
  78.     & (curDoc.DoCommand (commandID)) then begin
  79.         { document handled it }
  80.     end else if gApplication.DoCommand (commandID) then begin
  81.         { application handled it }
  82.     end else begin
  83.         menuID := HiWrd (menuChoice);
  84.         itemNr := LoWrd (menuChoice);
  85.         if menuID = MENU_Apple then begin
  86.             DoApple (itemNr);
  87.         end;
  88.     end;
  89.     HiliteMenu (0);
  90. end; {DoMenu}
  91.  
  92. {----------}
  93. Function  GetCommandFromMenu (
  94.     menuChoice:        longint): longint;
  95. var
  96.     commandID:        longint;
  97. Begin
  98.     commandID := 0;
  99.  
  100.     case menuChoice of
  101.         cAppleAbout:
  102.                 commandID := cmdInvokeAbout;
  103.         cFileNew:
  104.                 commandID := cmdNew;
  105.         cFileOpen:
  106.                 commandID := cmdOpen;
  107.         cFileClose:
  108.                 commandID := cmdClose;
  109.         cFileSave:
  110.                 commandID := cmdSave;
  111.         cFileSaveAs:
  112.                 commandID := cmdSaveAs;
  113.         cFileRevert:
  114.                 commandID := cmdRevert;
  115.         cFilePageSetup:
  116.                 commandID := cmdPageSetup;
  117.         cFilePrint:
  118.                 commandID := cmdPrint;
  119.         cFileQuit:
  120.                 commandID := cmdQuit;
  121.         cEditUndo:
  122.                 commandID := cmdUndo;
  123.         cEditCut:
  124.                 commandID := cmdCut;
  125.         cEditCopy:
  126.                 commandID := cmdCopy;
  127.         cEditPaste:
  128.                 commandID := cmdPaste;
  129.         cEditClear:
  130.                 commandID := cmdClear;
  131.         cEditSelectAll:
  132.                 commandID := cmdSelectAll;
  133.         cEditShowClipboard:
  134.                 commandID := cmdShowClipboard;
  135.  
  136.         otherwise
  137.                 commandID := -menuChoice;
  138.     end;
  139.  
  140.     GetCommandFromMenu := commandID;
  141. End;
  142.  
  143. {----------}
  144. Procedure DoApple (
  145.     itemNr:            integer);
  146. var
  147.     name:            Str255;
  148.     refNum:            integer;
  149. Begin
  150.     GetMenuItemText (AppleMenu, itemNr, name);
  151.     refNum := OpenDeskAcc (name);
  152. End;
  153.  
  154. {----------}
  155. var
  156.     menu:                MenuHandle;
  157.     menuBarChanged:        Boolean;
  158.  
  159. {----------}
  160. Procedure Enable (
  161.     itemNr:        integer;
  162.     enabled:    Boolean);
  163. Begin
  164.     if enabled then begin
  165.         EnableItem  (menu, itemNr);
  166.     end else begin
  167.         DisableItem (menu, itemNr);
  168.     end;
  169. end; {Enable}
  170.  
  171. {----------}
  172. Procedure EnableTitle (
  173.     menu:        MenuHandle;
  174.     enabled:    Boolean);
  175. Begin
  176.     if enabled <> BTst (menu^^.enableFlags, 0) then begin
  177.         menuBarChanged := true;
  178.     end;
  179.     if enabled then begin
  180.         EnableItem  (menu, 0);
  181.     end else begin
  182.         DisableItem (menu, 0);
  183.     end;
  184. end; {EnableTitle}
  185.  
  186. {----------}
  187. Procedure UpdateMenus;
  188. var
  189.     frontPeek:        WindowPeek;
  190.     isFront:        Boolean;    {is there a front window?}
  191.     isCur:            Boolean;    {is there a current window?}
  192.     isCurDoc:        Boolean;    {is there a current document?}
  193.     isDirty:        Boolean;    {is it dirty?}
  194.     hasFile:        Boolean;    {does it have a file?}
  195.     isSelected:        Boolean;    {is anything selected?}
  196.     isDesk:            Boolean;    {is the front window a desk acc?}
  197.     isText:            Boolean;    {is there a current text field?}
  198.     isScrap:        Boolean;    {is there any scrap?}
  199.     curTE:            TEHandle;
  200. Begin
  201.     menuBarChanged := false;
  202.  
  203.     isFront        := (FrontWindow () <> nil);
  204.     isCur        := (curWindow <> nil);
  205.     isDirty        := false;
  206.     hasFile        := false;
  207.     isSelected    := false;
  208.     isCurDoc    := cur.mDoc <> nil;
  209.     if isCurDoc then begin
  210.         isDirty        := cur.mDoc.mEngine.IsDirty;
  211.         hasFile        := cur.mDoc.mEngine.HasFile;
  212.     end;
  213.  
  214.     isDesk := false;
  215.     if isFront then begin
  216.         frontPeek    := WindowPeek (FrontWindow);
  217.         isDesk        := (frontPeek^.windowKind < 0);
  218.     end;
  219.  
  220.     curTE := cur.GetCurTE;
  221.  
  222.     isText        := isCur & (curTE <> nil);
  223.     isScrap        := false;
  224.     if isText then begin
  225.         isSelected    := (curTE^^.selStart <> curTE^^.selEnd);
  226.         isScrap        := (TEGetScrapLength > 0);
  227.     end;
  228.  
  229.     menu := FileMenu;
  230.     Enable (cFileClose,            isFront);
  231.     Enable (cFileSave,            isDirty);
  232.     Enable (cFileSaveAs,        isCurDoc);
  233.     Enable (cFileRevert,        isDirty);
  234.  
  235.     menu := EditMenu;
  236.     if isFront then begin
  237.         Enable (cEditUndo,        isDesk);
  238.         Enable (cEditCut,        isDesk | isSelected);
  239.         Enable (cEditCopy,        isDesk | isSelected);
  240.         Enable (cEditPaste,        isDesk | isScrap);
  241.         Enable (cEditClear,        isDesk | isSelected);
  242.         Enable (cEditSelectAll,    isText);
  243.  
  244.     end;
  245.     EnableTitle (EditMenu,     isFront);
  246.  
  247.  
  248.     if menuBarChanged then begin
  249.         DrawMenuBar;
  250.     end;
  251. end; {UpdateMenus}
  252.  
  253. end.
  254.